import org.annoflow.filter.sqli.*;
import org.annoflow.policy.Policy;
import org.annoflow.policy.boundry.SecureClassPolicy;
import org.zkoss.zk.ui.Executions;
import org.annoflow.filter.Filter;
import org.annoflow.filter.auth.*;

import student.web.Application;
import student.web.internal.CurrentUserMap;
import student.web.internal.SharedPersistenceMap;

public class LoginApp
{
	CurrentUserMap<User> currentUser = new CurrentUserMap<User>(User.class);
	SharedPersistenceMap<User> users	 = new SharedPersistenceMap<User>(User.class);
	
	public LoginApp()
	{
	}
	private String response = "";
	@Filter(type=LoginStatusFilter.class)
	public void markFail(String message)
	{
		response=message;
	}
	@Filter(type=SQLBoundFilter.class)
	public boolean login(String userName, String password)
	{
		User user = UserRegistry.authenticateUser(userName,password);
		if (user != null)
		{
			currentUser.setCurrentUser(user);
			
			Executions.sendRedirect("app.zhtml");
			return true;
		}
		markFail("Error Bad Credentials");
		return false;
	}
	public String getLoginResponse()
	{
		return response;
	}
	@Filter(type=SQLBoundFilter.class)
	public boolean createNewUser(String userName, String password)
	{
		User newUser = UserRegistry.createUser(userName,password);
		if(newUser == null)
		{
			markFail("Could Not Create New User");
			return false;
		}
		currentUser.setCurrentUser(newUser);
		Executions.sendRedirect("app.zhtml");
		return true;
		
		
	}

}
